<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class News
{
	private $db;
	private $table;
	
	private $tableColumns;
 
	private $acticleNav;
	private $columnsTitle;
	private $columnsDescription;
	private $columnsKeywords;
	
	public function __construct()
	{
		$this->db=new Mysql();
		$this->table = $this->db->pre.'news';
		
		$this->tableColumns = $this->db->pre.'columns';
		
	}
	public function getNews($id)
	{
		$sql = "SELECT a.*,c.cname FROM ".$this->table." a 
		
		LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
		WHERE a.id = $id ";
		return  $this->db->getRow($sql);
		
	}
	public function getNewsByCoulmns($cname,$nums=10)
	{
	    $sql = "SELECT id FROM ".$this->tableColumns." WHERE ename LIKE '$cname' ";
	
	    $columns = $this->db->selectOneCache($sql);
	
	    $sql = "SELECT a.*,c.cname,c.ename FROM ".$this->table." a
	    
	    LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
	    WHERE a.`status`=1 ";
	
	    $sql1 = "SELECT id FROM ".$this->tableColumns." WHERE pid = $columns ";
	    $c = $this->db->selectAllCache($sql1);
	    if ($c){
	        $cs='';
	        foreach ($c as $k=>$v){
	            $cs .= $v['id'].',';
	        }
	        $cid = substr($cs,0,strlen($cs)-1);
	
	        $sql = $sql." AND a.columns in ($cid) ";
	    }else{
	        $sql = $sql." AND a.columns in ($columns) ";
	    }
	    //		echo $sql;
	    $page = new Page();
	    $page->orderBy = 'a.id';
	    $page->uriStyle ='/';
	    $page->eachNums =$nums;	    
	    $list = $page->getPage($sql,1);
	    $html = $page->htmlPage();
	    return array('list'=>$list,'html'=>$html);
	}
	public function getTags($newsId=null)
	{
		if ($newsId){
			$sql = "SELECT * FROM ".$this->db->pre."tags WHERE `newsid` = $newsId ";
		}else{
			$sql = "SELECT * FROM ".$this->db->pre."tags  ";
		}
		return $this->db->getAll($sql);
	}
	public function getRelations($newsId=null){
		$sql = "SELECT * FROM ".$this->db->pre."news_relation WHERE newsid = $newsId ";
		return $this->db->getAll($sql);
	}
	public function del($id){
	
		
		$sql = "UPDATE ".$this->table." SET status = 2 WHERE id in ( $id )";
		return $this->db->update($sql);
		
	}
	public function add($data){
		$tags = trim(str_replace(array(';','；','，',','),';',trim($data['tags'])));		
		$ralation = trim(str_replace(array(';','；','，',','),';',trim($data['ralation'])));
		unset($data['tags']);
		unset($data['ralation']);
		
		$newsId = $this->db->insert($this->table, $data,true);
		if ($tags!=''){
			$tagsArr = explode(';', $tags);
			foreach ($tagsArr as $k=>$v){
				if ($v!=''){
					$data = array('newsid'=>$newsId,'name'=>$v,'abc'=>substr(pinYin($v,1),0,1));					
					$tagsId = $this->db->insert($this->db->pre.'tags', $data,true);					
					
				}
			}
		}
		if ($ralation!=''){
			$ralation = str_replace(';;', ';', $ralation);
			$raArr = explode(';', $ralation);			
			foreach ($raArr as $k=>$v){
				if ($v!=''){
					$data  = array('newsid'=>$newsId,'rid'=>$v);
					$this->db->insert($this->db->pre.'news_relation', $data);
				}
			}
		}
		$url = MVC_WWW_HOST.''.date("Y/md").'/'.$newsId.'.html';
		$sql = "UPDATE ".$this->table." SET `url` = '$url' WHERE id = $newsId limit 1 ";
		$this->db->update($sql);
		return $newsId;
	}
	public function update($newsId,$data)
	{
		$tags = trim(str_replace(array(';','；'),';',trim($data['tags'])));	
			
		$ralation = trim(str_replace(array(';','；'),';',trim($data['ralation'])));
		unset($data['tags']);
		unset($data['ralation']);
		$this->db->updateData($this->table, $data,"WHERE id = $newsId ");
		
		if ($tags!=''){
			$sql = "DELETE FROM ".$this->db->pre.'tags'." WHERE newsid = $newsId ";
			$this->db->query($sql);
			$tagsArr = explode(';', $tags);
			
			foreach ($tagsArr as $k=>$v){
				if ($v!=''){
					$data = array('newsid'=>$newsId,'name'=>$v,'abc'=>substr(pinYin($v,1),0,1));					
					$tagsId = $this->db->insert($this->db->pre.'tags', $data,true);					
					
				}
			}
		}
		if ($ralation!=''){
			$sql = "DELETE FROM ".$this->db->pre.'news_relation'." WHERE newsid = $newsId ";
			$this->db->query($sql);
			$ralation = str_replace(';;', ';', $ralation);
			$raArr = explode(';', $ralation);			
			foreach ($raArr as $k=>$v){
				if ($v!=''){
					$data  = array('newsid'=>$newsId,'rid'=>$v);
					$this->db->insert($this->db->pre.'news_relation', $data);
				}
			}
		}
		//$url = MVC_WWW_HOST.'html/'.date("Y/md").'/'.$newsId.'.html';
		//$sql = "UPDATE ".$this->table." SET `url` = '$url' WHERE id = $newsId limit 1 ";
		//$this->db->update($sql);
		return true;
	}
	
	
	
	public function toCate($newsId,$cateId)
	{
		$sql = "UPDATE ".$this->table." SET news_cate = '$cateId' WHERE id in( $newsId )";
		return $this->db->update($sql);
	}
	public function toColumns($newsId,$columnsId)
	{
		$sql = "UPDATE ".$this->table." SET columns = '$columnsId' WHERE id in( $newsId )";
		return $this->db->update($sql);
	}
	
	public function getNewsList($columns=null,$title=null){
		
		$sql = "SELECT a.*,c.cname FROM ".$this->table." a 
		
		LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
		WHERE a.`status`=1 ";
		if (!empty($columns)) {
			$sql2 = "SELECT id FROM ".$this->tableColumns." WHERE pid = $columns ";
			$c = $this->db->getAll($sql2);
			
			if ($c){
				$cs='';
				foreach ($c as $k=>$v){					
					$cs .= $v['id'].',';					
				}
				$cid = substr($cs,0,strlen($cs)-1);
				
				$sql = $sql." AND a.columns in ($cid) ";
			}else{
				$sql = $sql." AND a.columns in ($columns) ";
			}
			
		}
		if (!empty($title)) $sql = $sql." AND a.title like '%".$title."%' ";
		$page = new Page();
		$page->orderBy = 'a.id';
		$page->uriStyle ='?';
		$list = $page->getPage($sql);
		
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);
	
	}
	
	public function getNewsListBySearch()
	{
		$sql = "SELECT a.*,c.cname FROM ".$this->table." a 
		
		LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
		WHERE a.`status`=1 ";
		$columns = Http::get('columns');
		$createby = Http::get('createby');
		
		$createSTime = Http::get('createSTime');
		$createETime = Http::get('createETime');
		
		$title = Http::get('title');
		if (!empty($columns)) {
			$sql1 = "SELECT cname FROM ".$this->tableColumns." WHERE id = $columns ";
			$cinfo = $this->db->getRow($sql1);
			$_GET['cname']=$cinfo['cname'];
			$sql2 = "SELECT id FROM ".$this->tableColumns." WHERE pid = $columns ";
			$c = $this->db->getAll($sql2);			
			if ($c){
				$cs='';
				foreach ($c as $k=>$v){					
					$cs .= $v['id'].',';					
				}
				$cid = substr($cs,0,strlen($cs)-1);
				
				$sql = $sql." AND a.columns in ($cid) ";
			}else{
				$sql = $sql." AND a.columns in ($columns) ";
			}
			
		}
		if (!empty($createby)) $sql = $sql." AND a.createby like '%".$createby."%' ";
		if (!empty($createSTime)){ 
			$createSTime = strtotime($createSTime." 00:00:01");
			$sql = $sql." AND a.createTime > $createSTime ";
		}
		if (!empty($createETime)) {
			
			$createETime = strtotime($createETime." 23:59:59");
			$sql = $sql." AND a.createTime < $createETime ";
		}
		if (!empty($title)) $sql = $sql." AND a.title like '%".$title."%' ";
		//echo $sql;
		$page = new Page();
		$page->orderBy = 'a.id';
		$page->uriStyle ='?';
		$list = $page->getPage($sql);
		$html = $page->htmlPage();
		return array('list'=>$list,'html'=>$html);
	}
	public function getThisNav($columns,$globalArray)
	{
	
	    $sql = "SELECT * FROM ".$this->tableColumns." WHERE id = $columns ";
	    $row = $this->db->selectRow($sql);
	    $this->acticleNav.= ' &gt;  <a href="'.MVC_WWW_HOST.$row['ename'].'/index.html">'.$row['cname'].'</a>';
	
	    $this->columnsTitle.=$row['title'];
	    $this->columnsDescription .= $row['description'];
	    $this->columnsKeywords .= $row['keywords'];
	
	    if ($row['pid']>0)
	    {
	        $this->getThisNav($row['pid'], $globalArray);
	    }
	    $rs = '您现在的位置：<a href="'.MVC_WWW_HOST.'">'.$globalArray['webname'].'</a> '.$this->acticleNav;
	    $this->acticleNav = '';
	    if (trim($this->columnsTitle)=='') $this->columnsTitle=$globalArray['title'];
	    if (trim($this->columnsKeywords)=='') $this->columnsKeywords=$globalArray['keywords'];
	    if (trim($this->columnsDescription)=='') $this->columnsDescription=$globalArray['description'];
	
	    $arr = array('nav'=>$rs,'columnsTitle'=>$this->columnsTitle,'columnsKeywords'=>$this->columnsKeywords,'columnsDescription'=>$this->columnsDescription);
	
	    $this->columnsTitle='';
	    $this->columnsKeywords='';
	    $this->columnsDescription='';
	    return $arr;
	}
	public function getNewsById($id,$cache=1)
	{
	
	    $sql = "SELECT a.*,c.cname FROM ".$this->table." a
	    LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
	    WHERE a.id = '$id' ";
	    if ($cache){
	        $news =  $this->db->selectRowCache($sql);
	    }else{
	        $news =  $this->db->selectRow($sql);
	    }
	    $columns = $news['columns'];
	    $sql = "SELECT * FROM ".$this->tableColumns." WHERE id = $columns ";
	    if ($cache){
	        $columnsInfo = $this->db->selectRowCache($sql);
	    }else{
	        $columnsInfo = $this->db->selectRow($sql);
	    }
	
	    if (!empty($columnsInfo['pid'])){
	        $sql = "SELECT * FROM ".$this->tableColumns." WHERE id = ".$columnsInfo['pid']." ";
	        if ($cache){
	            $pinfo =  $this->db->selectRowCache($sql);
	        }else{
	            $pinfo =  $this->db->selectRow($sql);
	        }
	        $html = '<a href="'.MVC_WWW_HOST.$pinfo['ename'].'/index.html">'.$pinfo['cname'].'</a>'
	        .' > <a href="'.MVC_WWW_HOST.$pinfo['ename'].'/'.$columnsInfo['ename'].'/index.html">'.  $columnsInfo['cname'].'</a>';
	        $news['ename']=$pinfo['ename'];
	    }else{
	        $html = '<a href="'.MVC_WWW_HOST.$columnsInfo['ename'].'/index.html">'.  $columnsInfo['cname'].'</a>';
	        $news['ename']=$columnsInfo['ename'];
	    }
	    $news['navhtml']=$html;
	    return $news;
	}
	public function getTagsCache($newsId=null)
	{
	    if ($newsId){
	        $sql = "SELECT *  FROM ".$this->db->pre."tags WHERE `newsid` = $newsId ";
	    }else{
	        $sql = "SELECT *  FROM ".$this->db->pre."tags ";
	    }
	    return $this->db->selectAllCache($sql);
	}
	public function getRelationsCache($newsId=null){
	    $sql = "SELECT a.*,b.title,b.url,b.updateTime FROM ".$this->db->pre."news_relation  a LEFT JOIN ".$this->table." b ON a.rid = b.id  WHERE a.newsid = $newsId ";
	
	    return $this->db->selectAllCache($sql);
	}
	public function getNewsByCname($cname,$nums=10)
	{
	    $sql = "SELECT id FROM ".$this->tableColumns." WHERE ename LIKE '$cname' ";
	
	    $columns = $this->db->selectOneCache($sql);
	
	    $sql = "SELECT a.*,c.cname,c.ename FROM ".$this->table." a
	    
	    LEFT JOIN ".$this->tableColumns." c ON a.columns = c.id
	    WHERE a.`status`=1 ";
	
	    $sql1 = "SELECT id FROM ".$this->tableColumns." WHERE pid = $columns ";
	    $c = $this->db->selectAllCache($sql1);
	    if ($c){
	        $cs='';
	        foreach ($c as $k=>$v){
	            $cs .= $v['id'].',';
	        }
	        $cid = substr($cs,0,strlen($cs)-1);
	
	        $sql = $sql." AND a.columns in ($cid) ";
	    }else{
	        $sql = $sql." AND a.columns in ($columns) ";
	    }
	    //		echo $sql;
	    $page = new Page();
	    $page->orderBy = 'a.id';
	    $page->uriStyle ='/';
	    $page->eachNums =$nums;
	    $list = $page->getPage($sql,1);
	    $html = $page->htmlPage();
	    return array('list'=>$list,'html'=>$html);
	}
	function getAllArticleId(){
	    $sql = "SELECT id FROM ".$this->table." WHERE status=1 ";
	    $rs = $this->db->getAll($sql);
	    $id = array();
	    foreach ($rs as $k=>$v){
	        $id[]=$v['id'];
	    }
	    return $id;
	}
}